<!--
 * @Autor: Hongting Yuan
 * @Date: 2021-12-08 21:04:13
 * @LastEditors: Haiting Zhao
 * @LastEditTime: 2021-12-19 13:11:11
 * @LastEditors: Hongting Yuan
 * @LastEditTime: 2021-12-12 12:35:06
 * @Description: file content
 * @FilePath: \Management-of-Scientific-Research-in-Universities-Front\src\views\scientificexam\ExamResult.vue
-->

<template>
  <div>
    <!-- 面包屑导航区 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ name:'WorkPlace' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>科研考核</el-breadcrumb-item>
      <el-breadcrumb-item>考核结果</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card
      style="width: 100%;height: 100%"
    >

      <!-- 根据单位搜索 -->
      <el-select v-model="departmentvalue" style="margin-bottom: 15px;width: 260px; height: 40px;margin-right: 50px;" placeholder="请选择所属单位" clearable @change="getdepartmentId()">
        <el-option
          v-for="item in departmentList"
          :key="item.id"
          :label="item.name"
          :value="item.id"
        />
      </el-select>
      <!-- 根据项目名搜索 -->
      <el-input v-model="projectName" style="margin-bottom: 15px;margin-right: 10px;width: 260px; height: 40px;" placeholder="请输入项目名称" clearable @clear="getdepartmentByName">
        <el-button slot="append" icon="el-icon-search" @click="getdepartmentByName" />
      </el-input>
      <!-- 导出信息 -->
      <download-excel :data="resultList" :fields="excelFields" style="float: right;margin-bottom: 15px;">
        <el-button v-preventReClick="3000" type="primary" style="float: right;margin-bottom: 15px;" icon="el-icon-download">导出信息</el-button>
      </download-excel>
      <!-- 渲染获奖成果 -->
      <el-table
        :data="resultList"
        :default-sort="{prop: 'completeTime', order: 'descending'}"
        style="width: 100%;"
        :row-class-name="tableRowClassName"
      >
        <el-table-column
          prop="projectName"
          label="项目名"
          :show-overflow-tooltip="true"
          align="center"
        />
        <el-table-column
          prop="projectLeader"
          :show-overflow-tooltip="true"
          label="负责人"
          align="center"
        />
        <el-table-column
          prop="department"
          :show-overflow-tooltip="true"
          label="所属单位"
          align="center"
        />
        <el-table-column
          prop="startTime"
          :show-overflow-tooltip="true"
          sortable
          label="开始时间"
          align="center"
        />
        <el-table-column
          prop="completeTime"
          :show-overflow-tooltip="true"
          sortable
          label="结束时间"
          align="center"
        />
        <el-table-column
          prop="paperScore"
          :show-overflow-tooltip="true"
          sortable
          label="论文得分"
          align="center"
        />
        <el-table-column
          prop="awardedScore"
          :show-overflow-tooltip="true"
          sortable
          label="获奖得分"
          align="center"
        />
        <el-table-column
          prop="patentScore"
          :show-overflow-tooltip="true"
          sortable
          label="专利得分"
          align="center"
        />
        <el-table-column
          prop="levelScore"
          :show-overflow-tooltip="true"
          sortable
          label="项目等级得分"
          align="center"
        />
        <el-table-column
          prop="totalScore"
          :show-overflow-tooltip="true"
          sortable
          label="总分"
          align="center"
        />
      </el-table>
      <!-- 分页 -->
      <el-pagination
        style="text-align: right;"
        :current-page="currentPage"
        :page-sizes="[3, 5, 10, 20]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="totalNum"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>
  </div>
</template>
<script>
import axios from 'axios'
export default {
  name: 'ExamResult',
  data() {
    return {
      resultList: [],
      departmentList: [],
      departmentId: 0,
      departmentvalue: '',
      textDepartmentList: '',
      currentPage: 1,
      pageSize: 5,
      totalNum: 0,
      projectName: '',
      // 将表格中的信息导出为Excel的文件头
      excelFields: {
        '项目名': 'projectName',
        '项目负责人': 'projectLeader',
        '所属单位': 'department',
        '开始时间': 'startTime',
        '结束时间': 'completeTime',
        '论文得分': 'paperScore',
        '获奖得分': 'awardedScore',
        '专利得分': 'patentScore',
        '项目等级得分': 'levelScore',
        '总分': 'totalScore'
      }
    }
  },
  created() {
    // this.getdepartmentList()
    this.textDepartmentList = JSON.parse(this.$store.getters.getdepartment)
    for (var i = 0; i < this.textDepartmentList.length; i++) {
      if (this.textDepartmentList[i] != null) {
        this.departmentList.push(this.textDepartmentList[i])
      }
    }
    // console.log(this.departmentList)
    this.getResultList()
    // this.getdepatdata()
  },
  methods: {
    /**
     * @description: 获取考核结果列表
     * @param {*}
     * @return {*}
     * @author: Hongting Yuan
     */
    getResultList() {
      var self = this
      axios.get(`/assessment/getAllProject?pageNum=${this.currentPage}&pageSize=${this.pageSize}`)
        .then(res => {
          if (res.data.code !== 200) return this.$message.error('获取考核结果失败')
          // console.log(res)
          self.resultList = []
          if (res.data.data && res.data.data.list) {
            self.getList(res)
          }
        })
        .catch(err => {
          console.log(err)
        })
    },

    // getdepatdata() {
    //   // 各学院
    //   var depart = [
    //     '计算机科学学院',
    //     '工学院',
    //     '文学院',
    //     '数学学院',
    //     '商学院',
    //     '经济与管理学院',
    //     '法学院',
    //     '应用技术学院',
    //     '继续教育学院',
    //     '工学院',
    //     '历史文化与旅游学院',
    //     '心理学院',
    //     '化学与材料科学学院',
    //     '服装与设计艺术学院',
    //     '体育学院',
    //     '舞蹈学院',
    //     '音乐学院',
    //     '美术学院',
    //     '教育科学学院',
    //     '国际教育学院',
    //     '影视与传媒学院',
    //     '地理与资源科学学院',
    //     '生命科学学院',
    //     '物理与电子工程学院',
    //     '马克思主义学院',
    //     '外国语学院']
    //   axios.get(`/assessment/getAllProject?pageNum=1&pageSize=999`)
    //     .then(res => {
    //       var departnum = []
    //       var alllist = []
    //       var self = this
    //       if (res.data.code !== 200) return this.$message.error('获取考核结果失败')
    //       if (res.data.data && res.data.data.list) {
    //         alllist = self.getdepartList(res)
    //       }

    //       for (var i = 0; i < depart.length; i++) {
    //         departnum[i] = 0
    //       }

    //       for (i = 0; i < alllist.length; i++) {
    //         for (var j = 0; j < depart.length; j++) {
    //           if (alllist[i].department === depart[j]) {
    //             departnum[j]++
    //           }
    //         }
    //       }
    //       // 获取各学院的数量
    //       console.log(departnum)
    //     })
    //     .catch(err => {
    //       console.log(err)
    //     })
    // },
    getdepartList(res) {
      var resultList = []
      // self.totalNum = res.data.data.list.length
      self.totalNum = res.data.data.total
      for (var i = 0; i < res.data.data.list.length; i++) {
        const objectResult = {}
        objectResult.department = this.departmentList[res.data.data.list[i].projectVo.departmentId - 2].name
        resultList.push(objectResult)
      }
      return resultList
    },

    /**
     * @description: 将时间戳转为2021-11-07 21:31:13格式
     * @param {*} data
     * @return {*}
     * @author: Hongting Yuan
     */
    getdata(data) {
      var date = new Date(data)
      var Y = date.getFullYear() + '-'
      var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-'
      var D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate())
      return Y + M + D
    },
    /**
     * @description: 获取部门列表
     * @param {*}
     * @return {*}
     * @author: Hongting Yuan
     */
    // getdepartmentList() {
    //   axios.get('/dictionary/searchAll/department')
    //     .then(res => {
    //       if (res.data.code !== 200) return this.$message.error('获取部门列表失败')
    //       this.departmentList = res.data.data
    //       console.log(this.departmentList)
    //     })
    //     .catch(err => {
    //       console.error(err)
    //     })
    // },
    /**
     * @description: 根据部门搜索项目成果结果
     * @param {*}
     * @return {*}
     * @author: Hongting Yuan
     */
    getdepartmentId() {
      var self = this
      if (self.departmentvalue === '') {
        this.getResultList()
      } else {
        for (var i = 0; i < self.departmentList.length; i++) {
          // console.log(self.departmentList[i].id, self.departmentvalue)
          if (self.departmentList[i].id === self.departmentvalue) {
            axios.get(`/assessment/searchByDeId?pageNum=${self.currentPage}&pageSize=${self.pageSize}&departmentId=${self.departmentList[i].id}`)
              .then(res => {
                if (res.data.code !== 200) return this.$message.error('搜索考核结果失败')
                self.resultList = []
                if (res.data.data) {
                  self.getList(res)
                  self.totalNum = res.data.data.total
                }
                // console.log(self.resultList)
                // self.totalNum = self.resultList.length
              })
              .catch(err => {
                console.error(err)
              })
            break
          } else {
            self.totalNum = 0
          }
        }
      }
    },
    /**
     * @description: 将获取的数据转换为table中需要渲染的对象数组
     * @param {*} res
     * @return {*}
     * @author: Hongting Yuan
     */
    getList(res) {
      // this.totalNum = res.data.data.list.length
      this.totalNum = res.data.data.total
      for (var i = 0; i < res.data.data.list.length; i++) {
        const objectResult = {}
        objectResult.projectName = res.data.data.list[i].projectVo.projectName
        objectResult.projectLeader = res.data.data.list[i].projectVo.team[0].userRealname
        // if (res.data.data.list[i].projectVo.departmentId <= 1) {
        //   objectResult.department = self.departmentList[res.data.data.list[i].projectVo.departmentId - 1].departmentName
        // } else {
        objectResult.department = this.departmentList[res.data.data.list[i].projectVo.departmentId - 2].name
        // }
        objectResult.startTime = this.getdata(res.data.data.list[i].projectVo.startTime)
        objectResult.completeTime = this.getdata(res.data.data.list[i].projectVo.completeTime)
        objectResult.awardedScore = res.data.data.list[i].projectScore.awardedScore
        objectResult.levelScore = res.data.data.list[i].projectScore.levelScore
        objectResult.paperScore = res.data.data.list[i].projectScore.paperScore
        objectResult.patentScore = res.data.data.list[i].projectScore.patentScore
        objectResult.totalScore = res.data.data.list[i].projectScore.totalScore
        this.resultList.push(objectResult)
      }
    },
    /**
     * @description: 根据项目名搜索项目成果
     * @param {*}
     * @return {*}
     * @author: Hongting Yuan
     */
    getdepartmentByName() {
      var self = this
      if (self.projectName === '') {
        self.getResultList()
      } else {
        axios.get(`/assessment/searchByName?pageNum=${this.currentPage}&pageSize=${this.pageSize}&projectName=${this.projectName}`)
          .then(res => {
            if (res.data.code !== 200) return this.$message.error('搜索考核结果失败')
            // console.log(res)
            self.resultList = []
            if (res.data.data) {
              self.getList(res)
            } else {
              self.totalNum = 0
            }
            self.totalNum = res.data.data.total
            // console.log(self.resultList)
          })
          .catch(err => {
            console.error(err)
          })
      }
    },
    /**
     * @description: 页面展示数据条数发生变化
     * @param {*} val
     * @return {*}
     * @author: Hongting Yuan
     */
    handleSizeChange(val) {
      this.pageSize = val
      this.getResultList()
    },
    /**
     * @description: 当前显示页发生变化
     * @param {*} val
     * @return {*}
     * @author: Hongting Yuan
     */
    handleCurrentChange(val) {
      this.currentPage = val
      this.getResultList()
    },
    /**
     * @description: 设置每行颜色
     * @param {*} row
     * @param {*} rowIndex
     * @return {*}
     * @author: Hongting Yuan
     */
    /**
     * @description: 设置表格每行颜色
     * @param {*} row
     * @param {*} rowIndex
     * @return {*}
     * @author: Hongting Yuan
     */
    tableRowClassName({ row, rowIndex }) {
      if (rowIndex % 4 === 3) {
        return 'warning-row'
      } else if (rowIndex % 4 === 1) {
        return 'success-row'
      }
      return ''
    }
  }
}
</script>
<style lang="less">
  .el-table .warning-row {
    background: oldlace;
  }

  .el-table .success-row {
    background: #f0f9eb;
  }
</style>
